﻿using System;
using System.Diagnostics.Contracts;

namespace Isuka.Automation
{
    /// <summary>
    /// Silverlight にあるやつの代替
    /// </summary>
    public static class ComObjectFactory
    {
        /// <summary>
        /// COMオブジェクトの作成
        /// </summary>
        /// <param name="progId"></param>
        /// <param name="comServer"></param>
        /// <returns></returns>
        public static object CreateObject(string progId, string comServer = null)
        {
            Contract.Requires<ArgumentException>(!String.IsNullOrEmpty(progId));
            Contract.Ensures(Contract.Result<object>() != null);

            var t = string.IsNullOrEmpty(comServer)
                        ? Type.GetTypeFromProgID(progId, true)
                        : Type.GetTypeFromProgID(progId, comServer, true)
                        ;
            Contract.Assume(t != null);
            var obj = Activator.CreateInstance(t);
            return Gate.NotNull(obj, () => new ObjectCreationException(t));
        }
    }
}
